﻿using System;
using System.IO;
using System.Windows.Forms;
using System.Globalization;
using Microsoft.WindowsMobile.PocketOutlook;
using Microsoft.WindowsMobile.Status;
using MyMegafon.Properties;

namespace MyMegafon
{
    public class Balans
    {

        public double Balance;
        public double OldBalance;
        readonly SystemState _sysPhone = new SystemState(SystemProperty.PhoneSignalStrength, true);
        SmsMessage message = new SmsMessage();
        FileStream _stream;
        const string FileName = "previous";

        public Balans()
        {
            Balance = 0.00;
            OldBalance = 0.00;
        }
        public Balans(string a)
        {
            SetNowBalace(a);
        }

        public double GetBalanseDeltaOld()
        {
            return Balance - OldBalance;
        }

        public void SetNowBalace(string a)
        {
            OldBalance = Balance;
            a = a.Remove(a.Length - 4, 4);
            string doubleSep = NumberFormatInfo.CurrentInfo.NumberDecimalSeparator;
            a = a.Replace(".", doubleSep);
            a = a.Replace(",", doubleSep);
            try
            {
                Balance = double.Parse(a);
            }
            catch
            {
                MessageBox.Show("Ошибка конвертации" + a);
            }
            Database.SaveToDatabase(Balance);
        }

        public void Refresh()
        {
            RequirementsSmsBalans();
        }

        private void RequirementsSmsBalans()
        {
            if (message.To.Count == 0)
            {
                message.To.Add(new Recipient("000100"));
                message.To.Add(new Recipient("000102"));
                message.Body = " ";
            }
            try
            {
                message.Send();
            }
            catch
            {
                _sysPhone.Changed += sysPhone_Changed;
            }
        }

        int _a;
        private void sysPhone_Changed(object sender, ChangeEventArgs args) 
        {
            _a++;
            try
            {
                if (_a > 1)
                    return;
                message.Send();
                _sysPhone.Changed -= sysPhone_Changed;
                _a = 0;
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
                _a = 0;
            }
        }

        public string GetBalansFromFile()
        {
            StreamReader reader = null;
            string line = "";
            try {
                _stream = new FileStream(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase)
                    + "\\" + FileName, FileMode.Open, FileAccess.Read, FileShare.Read);
                    reader = new StreamReader(_stream);
                    line = reader.ReadLine();
                } 
                catch
                {}
                finally
                {
                    if (_stream != null)_stream.Close();
                    if (reader != null) reader.Close();
                }
            return line;
         }

        public void SaveBalansToFile()
        {
            StreamWriter writer = null;
            try
            {
               _stream = new FileStream(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase)
                  +"\\"+ FileName, FileMode.Create, FileAccess.Write);
                writer = new StreamWriter(_stream);
                writer.WriteLine(Balance.ToString());
                writer.Flush();
            }
            catch
            {
            }
            finally
            {
                if (_stream != null)_stream.Close();
                if (writer != null) writer.Close();
            }
        }
    }
}
